package org.zjt.agent.generate.javassist;

import javassist.CtMethod;
import org.zjt.agent.generate.ClassHandler;
import org.zjt.agent.generate.MethodRedefineStrategy;

import java.util.Objects;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-07-06 下午7:35
 * @see
 */
public class ThreadPoolAfterExecuteMethodRedefineStrategy implements MethodRedefineStrategy<CtMethod> {

    @Override
    public boolean support(CtMethod methodDescription) {
        try {
            return Objects.equals(methodDescription.getName(),"afterExecute") && Objects.nonNull(methodDescription.getParameterTypes())
                    && methodDescription.getParameterTypes().length == 2 && Objects.equals(methodDescription.getParameterTypes()[1].getName(),Runnable.class.getName())
                    && Objects.equals(methodDescription.getParameterTypes()[0].getName(),Thread.class.getName());
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void redefine(CtMethod methodDescription) {
        try {
            methodDescription.insertBefore("System.out.println(\" afterExecute before \");");
            methodDescription.insertAfter("System.out.println(\" afterExecute after \");");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Class<ThreadPoolClassHandler> getClassHandlerType() {
        return ThreadPoolClassHandler.class;
    }


}
